Imports System.Data.SqlClient

Public Class TipoEstudio : Implements IBusquedaFiltrado
    Private IdTipoEstudio As Integer
    Private Descrip As String
    Public Property TipoEstudio() As Integer
        Get
            Return IdTipoEstudio
        End Get
        Set(ByVal value As Integer)
            IdTipoEstudio = value
        End Set
    End Property
    Public Property Descripcion() As String
        Get
            Return Descrip
        End Get
        Set(ByVal value As String)
            Descrip = value
        End Set
    End Property

    Public Sub New()
        IdTipoEstudio = 0
        Descrip = ""
    End Sub
    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
                   Implements IBusquedaFiltrado.Buscar

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM TipoEstudio WHERE IdTipoEstudio ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            IdTipoEstudio = System.Convert.ToInt16(rs("IdTipoEstudio"))
            Descrip = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "INSERT INTO TipoEstudio (IdTipoEstudio,Descrip)" + _
              "  VALUES({0},'{1}')"
        sql = String.Format(sql, IdTipoEstudio, Descrip)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "UPDATE TipoEstudio " + _
              "  SET Descrip = '{0}'," + _
              "  WHERE IdTipoEstudio = {1}"
        sql = String.Format(sql, Descrip, IdTipoEstudio)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
            Implements IBusquedaFiltrado.Eliminar

        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "DELETE FROM TipoEstudio WHERE IdTipoEstudio ={0}"
        sql = String.Format(sql, id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function BuscarDatos(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = "SELECT * FROM TIPOESTUDIO ORDER BY descrip"
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "tipoestudio")


        Return datos
    End Function
    Public Function BuscarId(ByVal DescTE As String, ByRef con As ConexionBD) As Boolean

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM TIPOESTUDIO WHERE Descrip='{0}'", DescTE)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            IdTipoEstudio = System.Convert.ToInt16(rs("idTipoEstudio"))
            Descrip = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
End Class
